package com.cy.pj.sys.service.impl;

import com.cy.pj.common.vo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.entity.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.cy.pj.sys.service.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * The type Sys log service.
 *
 * @author zimin
 * @create 2019 -07-09 9:18
 */
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @Override
    public PageObject<SysLog> findPageObject(String name, Integer pageCurrent) {
        //1.判定参数有效性,合法性
        if (pageCurrent==null||pageCurrent<1) {
            throw new IllegalArgumentException("当前页码不正确");
        }
        //2.基于用户名查询总记录数,并进行判定
        int rowCount = sysLogDao.getRowCount(name);
        if (rowCount == 0) {//return null;
            throw new ServiceException("没有找到记录");
        }
        //3.查询当前页记录
        int pageSize = 3;
        int startIndex = (pageCurrent-1)*pageSize;
        List<SysLog> records = sysLogDao.findPageObjects(name, startIndex, pageSize);
        //4.对查询结果进行封装并返回(返回给控制层)
//        PageObject<SysLog> slpo = new PageObject<>();
//        slpo.setRowCount(rowCount);
//        slpo.setRecords(records);
//        slpo.setPageSize(pageSize);
//        slpo.setPageCurrent(pageCurrent);
//        slpo.setPageCount((rowCount - 1) / pageSize + 1);
//        return slpo;
        return new PageObject<>(pageCurrent,pageSize,rowCount,(rowCount - 1) / pageSize + 1,records);
    }
}
